jetcrab\lexer\tokens/
keywords.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
4pub enum Keyword {
5    Let,
6    Const,
7    Var,
8    Function,
9    Return,
10    If,
11    Else,
12    For,
13    While,
14    Do,
15    Break,
16    Continue,
17    Switch,
18    Case,
19    Default,
20    Try,
21    Catch,
22    Finally,
23    Throw,
24    Class,
25    Extends,
26    Super,
27    New,
28    This,
29    Import,
30    Export,
31    From,
32    As,
33    Typeof,
34    Instanceof,
35    In,
36    Of,
37    Delete,
38    Void,
39    Yield,
40    Await,
41    Async,
42    Get,
43    Set,
44    Static,
45    Private,
46    Public,
47    Protected,
48    Interface,
49    Implements,
50    Enum,
51    Debugger,
52    With,
53    Strict,
54}
55
56impl Keyword {
57    pub fn as_str(&self) -> &'static str {
58        match self {
59            Keyword::Let => "let",
60            Keyword::Const => "const",
61            Keyword::Var => "var",
62            Keyword::Function => "function",
63            Keyword::Return => "return",
64            Keyword::If => "if",
65            Keyword::Else => "else",
66            Keyword::For => "for",
67            Keyword::While => "while",
68            Keyword::Do => "do",
69            Keyword::Break => "break",
70            Keyword::Continue => "continue",
71            Keyword::Switch => "switch",
72            Keyword::Case => "case",
73            Keyword::Default => "default",
74            Keyword::Try => "try",
75            Keyword::Catch => "catch",
76            Keyword::Finally => "finally",
77            Keyword::Throw => "throw",
78            Keyword::Class => "class",
79            Keyword::Extends => "extends",
80            Keyword::Super => "super",
81            Keyword::New => "new",
82            Keyword::This => "this",
83            Keyword::Import => "import",
84            Keyword::Export => "export",
85            Keyword::From => "from",
86            Keyword::As => "as",
87            Keyword::Typeof => "typeof",
88            Keyword::Instanceof => "instanceof",
89            Keyword::In => "in",
90            Keyword::Of => "of",
91            Keyword::Delete => "delete",
92            Keyword::Void => "void",
93            Keyword::Yield => "yield",
94            Keyword::Await => "await",
95            Keyword::Async => "async",
96            Keyword::Get => "get",
97            Keyword::Set => "set",
98            Keyword::Static => "static",
99            Keyword::Private => "private",
100            Keyword::Public => "public",
101            Keyword::Protected => "protected",
102            Keyword::Interface => "interface",
103            Keyword::Implements => "implements",
104            Keyword::Enum => "enum",
105            Keyword::Debugger => "debugger",
106            Keyword::With => "with",
107            Keyword::Strict => "strict",
108        }
109    }
110
111    pub fn from_string(s: &str) -> Option<Self> {
112        match s {
113            "let" => Some(Keyword::Let),
114            "const" => Some(Keyword::Const),
115            "var" => Some(Keyword::Var),
116            "function" => Some(Keyword::Function),
117            "return" => Some(Keyword::Return),
118            "if" => Some(Keyword::If),
119            "else" => Some(Keyword::Else),
120            "for" => Some(Keyword::For),
121            "while" => Some(Keyword::While),
122            "do" => Some(Keyword::Do),
123            "break" => Some(Keyword::Break),
124            "continue" => Some(Keyword::Continue),
125            "switch" => Some(Keyword::Switch),
126            "case" => Some(Keyword::Case),
127            "default" => Some(Keyword::Default),
128            "try" => Some(Keyword::Try),
129            "catch" => Some(Keyword::Catch),
130            "finally" => Some(Keyword::Finally),
131            "throw" => Some(Keyword::Throw),
132            "class" => Some(Keyword::Class),
133            "extends" => Some(Keyword::Extends),
134            "super" => Some(Keyword::Super),
135            "new" => Some(Keyword::New),
136            "this" => Some(Keyword::This),
137            "import" => Some(Keyword::Import),
138            "export" => Some(Keyword::Export),
139            "from" => Some(Keyword::From),
140            "as" => Some(Keyword::As),
141            "typeof" => Some(Keyword::Typeof),
142            "instanceof" => Some(Keyword::Instanceof),
143            "in" => Some(Keyword::In),
144            "of" => Some(Keyword::Of),
145            "delete" => Some(Keyword::Delete),
146            "void" => Some(Keyword::Void),
147            "yield" => Some(Keyword::Yield),
148            "await" => Some(Keyword::Await),
149            "async" => Some(Keyword::Async),
150            "get" => Some(Keyword::Get),
151            "set" => Some(Keyword::Set),
152            "static" => Some(Keyword::Static),
153            "private" => Some(Keyword::Private),
154            "public" => Some(Keyword::Public),
155            "protected" => Some(Keyword::Protected),
156            "interface" => Some(Keyword::Interface),
157            "implements" => Some(Keyword::Implements),
158            "enum" => Some(Keyword::Enum),
159            "debugger" => Some(Keyword::Debugger),
160            "with" => Some(Keyword::With),
161            "strict" => Some(Keyword::Strict),
162            _ => None,
163        }
164    }
165
166    pub fn matches_str(&self, s: &str) -> bool {
167        self.as_str() == s
168    }
169}
170
171impl PartialEq<&str> for Keyword {
172    fn eq(&self, other: &&str) -> bool {
173        self.as_str() == *other
174    }
175}
176
177impl PartialEq<str> for Keyword {
178    fn eq(&self, other: &str) -> bool {
179        self.as_str() == other
180    }
181}